home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / Tabs LDEF 1.0 / Tabs LDEF ƒ / Tabs LDEF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-15  |  4.1 KB  |  180 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Tabs LDEF.c
  3.  
  4.     Contains:    LDEF that supports tabbed text
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.             12/18/95            CW        First release
  13.  
  14. */
  15.  
  16.  
  17.  
  18.  
  19. #include <Types.h>
  20. #include <Lists.h>
  21. #include <LowMem.h>
  22.  
  23. #define    USE_LDEF
  24.  
  25.  
  26.  
  27. #ifndef USE_LDEF
  28. pascal void tabsLDEF ( short theMessage, Boolean bSelect, Rect* theRect, Cell theCell,
  29.                            short theOffset, short theLen, ListRef theList );
  30. #endif
  31.  
  32.  
  33. void DrawCell ( short theMessage, Boolean bSelect, Rect* cellRect, Cell theCell,
  34.                     ListRef theHandle, short (*tabsOffset)[] );
  35. void HiliteRect ( Rect* rect );
  36. void AdjustStringWidth ( StringPtr theString, int availableWidth );
  37.  
  38.  
  39.  
  40.  
  41. #define    kStdIndent        2
  42.  
  43.  
  44.  
  45. #ifndef USE_LDEF
  46.  
  47. pascal void tabsLDEF ( short theMessage, Boolean bSelect, Rect* theRect, Cell theCell,
  48.                            short theOffset, short theLen, ListRef theList )
  49.                    
  50. #else
  51.  
  52. pascal void main ( short theMessage, Boolean bSelect, Rect* theRect, Cell theCell,
  53.                    short theOffset, short theLen, ListRef theList )
  54.                    
  55. #endif
  56.  
  57. {
  58.     
  59.     short (*tabsOffset)[];
  60.     
  61.     switch ( theMessage )
  62.     {
  63.         case lDrawMsg:
  64.             // First, we need to grab hold of the tab positions
  65.             tabsOffset = (short (*)[]) (*theList)->refCon;
  66.             DrawCell (  theMessage, bSelect, theRect, theCell, theList, tabsOffset );
  67.         break;
  68.         
  69.         case lHiliteMsg:
  70.             HiliteRect ( theRect );
  71.         break;
  72.     }
  73.     
  74.     return;
  75. }
  76.  
  77.  
  78.  
  79. void DrawCell ( short theMessage, Boolean bSelect, Rect* cellRect, Cell theCell,
  80.                     ListRef theList, short (*tabsOffset)[] )
  81. {
  82.     SInt16        theLen;
  83.     int            lastTabOffset = (*tabsOffset)[1];
  84.     int            theIndex = 1;
  85.     int            i, j;
  86.     int            availableWidth;
  87.     FontInfo    fontInfo;
  88.     Str255        theString, theBit;
  89.     
  90.     
  91.     // Erase the cell area first
  92.     EraseRect ( cellRect );
  93.     
  94.     // Get current font information
  95.     GetFontInfo ( &fontInfo );
  96.     
  97.     // Lock down the cell data
  98.     HLock ( (*theList)->cells );
  99.     
  100.     // Get the cell text
  101.     theLen = 255;
  102.     LGetCell ( &theString[1], &theLen, theCell, theList );
  103.     theString[0] = theLen;
  104.     
  105.     // Check for '\t' and draw the strings
  106.     for ( i = 1, j = 1; i <= theString[0] + 1; i++, j++ )
  107.     {
  108.         // Assumes no '\t' on end, so also test for the last column
  109.         if ( theString[i] == '\t' || i == theString[0] + 1 )
  110.         {
  111.             // Move to the correct location, taking into account the
  112.             // font information. DrawString draws on the font base line.
  113.             MoveTo ( kStdIndent + cellRect->left + (*tabsOffset)[theIndex++],
  114.                         cellRect->top + fontInfo.ascent + fontInfo.leading - 1 );
  115.             theBit[0] = i - lastTabOffset - 1;
  116.             
  117.             // If this is the last column, calculate the available width using
  118.             // the list's view rect. Otherwise, we use the next tab position.
  119.             if ( i == theString[0] + 1 )
  120.                 availableWidth = (*theList)->rView.right - (*theList)->rView.left
  121.                                                          - (*tabsOffset)[theIndex - 1];
  122.             else
  123.                 availableWidth = (*tabsOffset)[theIndex] - (*tabsOffset)[theIndex - 1];
  124.             AdjustStringWidth ( theBit, availableWidth );
  125.             DrawString ( theBit );
  126.             lastTabOffset = i;
  127.             j = 0;            // Should be 1, but about to be incremented
  128.         }
  129.         else
  130.             theBit[j] = theString[i];
  131.     }
  132.     
  133.     //    Hiliting - first one or when scrolling
  134.     if ( bSelect )
  135.         HiliteRect ( cellRect );
  136.     
  137.     // Unlock the cell data
  138.     HUnlock ( (*theList)->cells );
  139.     
  140.     return;
  141. }
  142.  
  143.  
  144.  
  145. void HiliteRect ( Rect* rect )
  146. {
  147.     // Setup the hilite mode for color
  148.     LMSetHiliteMode ( LMGetHiliteMode ( ) & ~(1 << hiliteBit) );
  149.     // Invert the cell rectangle
  150.     InvertRect ( rect );
  151.     
  152.     return;
  153. }
  154.  
  155.  
  156.  
  157. void AdjustStringWidth ( StringPtr theString, int availableWidth )
  158. {
  159.     int currentWidth;
  160.     
  161.     
  162.     // Since we don't want the text to draw right up to
  163.     // the column edge, we include a little extra space.
  164.     availableWidth -= CharWidth ( ' ' );
  165.     
  166.     currentWidth = StringWidth ( theString );
  167.     while ( currentWidth > availableWidth && theString[0] )
  168.     {
  169.         // We could use CharWidth ( '…' ) and take that away from currentWidth.
  170.         // However, the width of a text segment may be different from the sum
  171.         // of its individual char widths, so this approach may be more acurate. 
  172.         theString[--theString[0]] = '…';
  173.         currentWidth = StringWidth ( theString );
  174.     }
  175.     
  176.     return;
  177. }
  178.  
  179.  
  180.